package io.finer.erp.jeecg.bas.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.math.MathUtil;

public class GetSexUtils {
	private static boolean f = true;
	Map<String,String> map=new HashMap<String,String>();
	
	public GetSexUtils() {
		this.f=true;
		this.map=new HashMap<String,String>();
	}

	public Map<String,String> getSexFA(String[] strings,Double tarNum) {
		//		String[] strings = new String[]{"25","26","26","26","27","27","27","27","27","28","28","28","29","30","30"};
		String coResult="";
		while(f) {
			String[] s_array = comb(strings,tarNum);
			if(s_array != null) {
				String ss = "";
				for(int i=0;i<s_array.length;i++) {
					if(ss.equals("")) {
						ss=s_array[i];
					}else {
						ss += "," + s_array[i];
					}
				}
				System.out.println("组合==="+ss);
				if(coResult.equals("")) {
					coResult=ss;
				}else {
					coResult+=";"+ss;
				}
				for(int j=0;j<strings.length;j++) {
					for(int i=0;i<s_array.length;i++) {
						if(strings[j] == s_array[i]) {
							strings[j] = "0";
							s_array[i] = "-1";
						}
					}
				}
				List<String> list = new ArrayList<String>();
				for(int i=0;i<strings.length;i++) {
					if(null!=strings[i]) {
						if(Double.parseDouble(strings[i])!=Double.parseDouble("0")) {
							list.add(strings[i]);
						}
					}
				}
				strings = list.toArray(new String[0]);
			}else {
				f = false;
			}
		}
		String ss = "";
		for(int i=0;i<strings.length;i++) {
			if(ss.equals("")) {
				ss=strings[i];
			}else {
				ss += "," + strings[i];
			}
		}
		System.out.println("剩余的数："+ss);
		map.put("uncorrect", ss);
		map.put("correct", coResult);
		return map;
	}

	public static void main(String[] args) {
        String [] array= {"23.0","24.0","25.0","26.0","27.0","28.0","29.0"};
		double targetNum= 108.0;
		new GetSexUtils().getSexFA(array,targetNum);
	}

	public static String[] comb(String[] strings,Double num) {
		for (int i = 1; i < strings.length; i++) {
			List<String[]> list = MathUtil.combinationSelect(strings, i);
			for(String[] s_array : list) {
				Double nnum = 0.0;
				for(String s : s_array) {
					if(null!=s) {
						Double snum = Double.parseDouble(s);
						nnum = nnum + snum;
					}
				}
				if(num>Double.parseDouble("109")) {
					if(nnum > num-40&&nnum <=num) {
						return s_array;
					}
				}else {
					if(nnum == num || nnum == (num-1)) {
						return s_array;
					}
				}
			}
		}
		f = false;
		return null;
	}


	public static double binarysearchKey(Double[] array, Double targetNum) {

		//Object[] array = temp.clone();
		Arrays.sort(array);
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		int targetindex = 0;
		int left = 0,right = 0; 
		for (right = array.length-1; left!=right;) {
			int midIndex = (right + left)/2;
			int mid = (right - left);
			double midValue = (Double) array[midIndex];
			if (targetNum == midValue) {
				return midIndex;
			}
			if(targetNum > midValue){
				left = midIndex;
			}else{
				right = midIndex;
			}

			if (mid <=1) {
				break;
			}
		}
		double rightnum = ((Double) array[right]);
		double leftnum = ((Double) array[left]);
		double ret =  Math.abs((rightnum - leftnum)/2) > Math.abs(rightnum -targetNum) ? rightnum : leftnum;
		System.out.println("和要查找的数："+targetNum+ "最接近的数：" + ret);
		return ret;
	}

}
